/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.api.listener;

import org.activiti.engine.EngineServices;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.swan.bean.MessageBean;

import com.koron.workflow.api.bean.RelationBean;
import com.koron.workflow.api.mapper.CommunicatingMapper;
import com.koron.workflow.api.service.RelationService;
import com.koron.workflow.api.service.TaskListService;
import com.koron.workflow.common.util.BusinessCode;
import com.koron.workflow.common.util.SessionFactoryLocal;
import com.koron.workflow.common.util.SpringContextHolder;

/**
 * 任务已完成监听器
 * @author 周利斌
 *
 */
public class TaskCompletedListener extends ProcessListenerAdapter{
	
	private static Logger log = LoggerFactory.getLogger(TaskCompletedListener.class);
	
	private RelationService relationService = SpringContextHolder.getBean(RelationService.class);
	
	private TaskListService taskListService = SpringContextHolder.getBean(TaskListService.class);
	
	@Override
	public void taskCompleted(ActivitiEvent event) {
		log.info("任务创建监听器已执行.....");
		EngineServices engineServices = event.getEngineServices();
		RuntimeService runtimeService = engineServices.getRuntimeService();
		String preTaskId = (String) runtimeService.getVariable(event.getExecutionId(), "taskId");
		String operator = (String) runtimeService.getVariable(event.getExecutionId(), "operator");
		String operation = (String) runtimeService.getVariable(event.getExecutionId(), "operation");
		SessionFactory factory = SessionFactoryLocal.get();
		//将任务列表的状态修改为已处理
		MessageBean<?> message = taskListService.updateTaskList(factory, preTaskId, operator);
		if(message.getCode() != BusinessCode.SUCCESS.getCode()) {
			log.error("将任务列表状态修改为已办失败：{}", message.getDescription());
			throw new RuntimeException(message.getDescription());
		}
		//将人员关系列表状态修改为已办
		message = relationService.updateRelation(factory, Integer.parseInt(operation), new RelationBean(RelationBean.R_HANDLE, null, event.getProcessInstanceId(), preTaskId, RelationBean.STATUS_HANDLED), null);
		if(message.getCode() != BusinessCode.SUCCESS.getCode()) {
			log.error("修改关系状态失败：{}", message.getDescription());
			throw new RuntimeException(message.getDescription());
		}
		//清除任务的沟通信息
		factory.getMapper(CommunicatingMapper.class).deleteByTaskId(preTaskId);
	}
}
